package GUILayer;
import ControlLayer.CtrDiscount;
import ModelLayer.Discount;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;

public class GUIDiscount extends javax.swing.JFrame {
    private CtrDiscount ctrDiscount;

    public GUIDiscount() {
        initComponents();
        ctrDiscount = CtrDiscount.getInstance();
        loadTableContent();
    }

    private void loadTableContent() {
        ((DefaultTableModel)tblDiscountList.getModel()).setRowCount(0);
        ArrayList<Discount> discountList = ctrDiscount.getDiscountList();
        for (Discount discount : discountList) {
            String categoryIDs = new String();
            for (Integer categoryID : discount.getCategoryIDs()) {
                categoryIDs += categoryID + ", ";
            }
            String orderIDs = new String();
            for (Integer orderID : discount.getOrderIDs()) {
                orderIDs += orderID + ", ";
            }
            ((DefaultTableModel)tblDiscountList.getModel()).addRow(
                new Object [] {
                    discount.getIDDiscount(),
                    discount.getIDDiscountType(),
                    discount.getIDCustomer(),
                    discount.getName(),
                    discount.getValue(),
                    discount.getQuantity(),
                    discount.getQuantityPerUser(),
                    discount.getCumulable(),
                    discount.getCumulableReduction(),
                    discount.getDateFrom(),
                    discount.getDateTo(),
                    discount.getMinimal(),
                    discount.getActive(),
                    discount.getDescription(),
                    categoryIDs,
                    orderIDs
                }
            );
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrlDiscountList = new javax.swing.JScrollPane();
        tblDiscountList = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblDiscountList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "id_discount_type", "id_customer", "name", "value", "quantity", "quantity_per_user", "cumulable", "cumulable_reduction", "date_from", "date_to", "minimal", "active", "description", "category IDs", "order IDs"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.Float.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Float.class, java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrlDiscountList.setViewportView(tblDiscountList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlDiscountList, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlDiscountList, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIDiscount().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scrlDiscountList;
    private javax.swing.JTable tblDiscountList;
    // End of variables declaration//GEN-END:variables
}
